//
//  RouterPrivateHandler.m
//  Pods
//
//  Created by Demi on 26/04/2017.
//
//

#import "RouterPrivateHandler.h"
#import <TXFire/TXFire.h>

@interface RouterPrivateHandler ()

@property (nonatomic, copy) NSString *pathPattern;
@property (nonatomic, copy) RouterHandlerBlock block;

@end

@implementation RouterPrivateHandler

- (instancetype)initWithPathPattern:(NSString *)pattern handle:(id  _Nullable (^)(RouterRequest * _Nonnull))handle
{
    if (self = [super init])
    {
        _pathPattern = pattern.copy;
        _block = [handle copy];
    }
    return self;
}

- (id)request:(RouterRequest *)request
{
    NSString *path = request.URL.path;
    if (!path)
    {
        return nil;
    }
    if (!TXRegexMatch(self.pathPattern, path))
    {
        return nil;
    }
    return self.block(request);
}

@end
